<template>
  <div>
    <div>
      <div class="header flex">
        <el-date-picker
      v-model="mounth"
      type="date"
      placeholder="选择日期"
      value-format="yyyy-MM-dd" v-if="$store.getters.isShow">
    </el-date-picker>
        <el-button @click="search()" type="primary" icon="el-icon-zoom-in" v-if="$store.getters.isShow">查询</el-button>
        <el-button @click="cleartIn()" type="primary"  icon="el-icon-refresh-left" v-if="$store.getters.isShow">重置</el-button>
        <el-button type="primary" @click="add()" icon="el-icon-plus">新增</el-button>
        <el-button type="danger" @click="clear()" icon="el-icon-delete" v-if="$store.getters.isShow">删除</el-button>

        <!-- 弹框  新增-->
        <el-dialog title="申请转正" :visible.sync="dialogFormVisible">
  <el-form :model="form">
    <el-form-item label="申请内容" :label-width="formLabelWidth">
      <el-input   type="textarea" :rows="2"
        placeholder="请输入内容" v-model="form.content" autocomplete="off"></el-input>
    </el-form-item>
    <el-form-item label="申请日期" :label-width="formLabelWidth">
      <el-date-picker
      v-model="form.applyDate"
      type="date"
      placeholder="选择日期"
      value-format="yyyy-MM-dd" >
    </el-date-picker>
    </el-form-item>
  </el-form>
  <div slot="footer" class="dialog-footer">
    <el-button @click="dialogFormVisible = false">取 消</el-button>
    <el-button type="primary" @click="submitForm(form)">确 定</el-button>

  </div>
</el-dialog>
      </div>
      <!-- 下面表格 -->
      <el-table
        ref="multipleTable"
        :data="tableData"
        tooltip-effect="dark"
        style="width: 100%"
        :header-cell-style="{background:'#eef1f6',color:'#606266'}"
        border
        @selection-change="handleSelectionChange">
      <!-- 单选全选 点击按钮 -->
        <el-table-column type="selection" width="55"> </el-table-column>
        <el-table-column label="申请人" width="120">
          <template slot-scope="scope">{{ scope.row.people }}</template>
        </el-table-column>
        <el-table-column prop="name" label="申请日期" width="120">
          <template slot-scope="scope">{{ scope.row.applyDate }}</template>
        </el-table-column>
        <el-table-column prop="address" label="申请内容">
          <template slot-scope="scope">{{ scope.row.content }}</template>
        </el-table-column>
        <el-table-column label="审批人" width="120">
          <template slot-scope="scope">{{ scope.row.auditPeople }}</template>
        </el-table-column>
        <el-table-column prop="address" label="审批时间" show-overflow-tooltip>
          <template slot-scope="scope">{{ scope.row.auditDate }}</template>
        </el-table-column>
        <el-table-column label="审批结果" width="120">
          <template slot-scope="scope">
          <!-- <div slot="reference" class="name-wrapper"> -->
            <el-tag size="50" :type="scope.row.type2" effect="dark">{{ scope.row.opinion }}</el-tag>
          <!-- </div> -->
      </template>
        </el-table-column>
        <el-table-column label="审批意见" width="120">
          <template slot-scope="scope">{{ scope.row.result }}</template>
        </el-table-column>
        <el-table-column label="状态" width="120">
          <template slot-scope="scope">
          <!-- <div slot="reference" class="name-wrapper"> -->
            <el-tag size="50" :type="scope.row.type2" effect="dark">{{ scope.row.type }}</el-tag>
          <!-- </div> -->
      </template>
        </el-table-column>
        <el-table-column prop="address" label="创建时间" show-overflow-tooltip>
          <template slot-scope="scope">{{ scope.row.createDate }}</template>
        </el-table-column>
        <el-table-column label="操作" v-if="$store.getters.isShow">
      <template slot-scope="scope">
        <el-button
          size="60"
          @click="handleEdit(scope.row)" type="danger">审批</el-button>
      </template>
      </el-table-column>
      </el-table>
      <!-- 审批 -->
      <el-dialog title="审批" :visible.sync="dialogFormVisible2">
  <el-form :model="form2">
    <el-radio v-model="form2.radio" label="通过">通过</el-radio>
  <el-radio v-model="form2.radio" label="不通过">不通过</el-radio>
  <el-input   type="textarea" :rows="2"
        placeholder="请输入意见" v-model="form2.opinion" autocomplete="off"></el-input>
  </el-form>
  <div slot="footer" class="dialog-footer">
    <el-button @click="dialogFormVisible2 = false">取 消</el-button>
    <el-button type="primary" @click="submitForm2(form2)">确 定</el-button>
  </div>
</el-dialog>
      <Pagin :count="total" @pageEvent="fetchData" :pageSize="pageSize"></Pagin>
    </div>
  </div>
</template>

<script>
import {applListAPI,addAppInsertAPI,appUpdateAPI,appDeleteAPI,appSeach} from '../../api/apply'
export default {
  data() {
    return {
      form: {
        applyDate: '',
        content: ''
        },
        formLabelWidth: '120px',
      type:'danger',
      dialogFormVisible: false,
      dialogFormVisible2: false,//修改
      form2: {},
      dataList:[],
      total: 0, // 列表总行数
      currentPage: 1,
      pageSize: 10,
      tableData: [],
      multipleSelection: [],
      ids : [] ,// 审批id
      mounth:'',// 查询
      isShow:''
    };
  },
  created(){
    this.jia()
  },
  components: {
    Pagin: () => import('c/Pagin')
  },
  methods: {
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    //重置
    cleartIn(){
      this.mounth =''
    },
    //查询
   async  search(){
  const res = await appSeach({
    applyDate  : this.mounth
  })
  if(res.code == '1'){
        if(res.data != null){
          this.dataList = res.data
          this.total = this.dataList.length
          this.fetchData(this.currentPage)
          this.$message.success(res.msg)
        }
      }else{
          this.$message.error(res.msg)
        }
    },
   async submitForm(form) {
    const res = await addAppInsertAPI({
      applyDate : form.applyDate,
      content  : form.content
    })
      //请求成功后
      if(res.code == '1'){
        this.jia()
        this.$message.success('添加成功')
       }else{
        this.$message.error('添加失败')
       }
      //关闭
      this.dialogFormVisible = false
    },
    //删除
   async clear(){
      var id = []
      this.multipleSelection.forEach(item=>{
         id.push(item.id)
      })
      //请求成功后
      if(id.length > 0){
        const res = await appDeleteAPI(id)
         //请求成功后
         if(res.code == '1'){
        this.jia()
        this.$message.success('删除成功')
       }else{
        this.$message.error('删除失败')
       }
      }
    },
    //审批确定
   async submitForm2(form){
    const res = await appUpdateAPI({
      id : this.form2.id,
      auditDate : this.form2.auditDate ,
      result : form.radio ,
      opinion : form.opinion ,
      type  : form.radio
    })
    if(res.code == '1'){
        this.jia()
        this.$message.success('审批成功')
       }else{
        this.$message.error('审批失败')
       }
      this.dialogFormVisible2 = false

    },
      //新增
      add(){
      this.dialogFormVisible = true
    },
    //审核
    handleEdit(row){
         this.dialogFormVisible2 = true
         this.form2.id = row.id
    },
    //模拟假数据
   async jia(){
    const res =  await applListAPI()
    res.data.map(item=>{
      if(item.result == '通过'){
        item.type2 = 'success'
      }else{
        item.type2 = 'danger'
      }
    })
    console.log(res.data);
      this.dataList = res.data
      this.total = this.dataList.length
      this.fetchData(this.currentPage)
    },
    fetchData (count) {
      var list = this.dataList
      var from = (count - 1) * this.pageSize
      const to = count * this.pageSize
      this.tableData = []
      for (; from < to; from++) {
        if (list[from]) {
          this.tableData.push(list[from])
        }
      }
    },
  },
};
</script>
<style scoped lang="scss">
.flex {
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.input {
  width: 120px;
}
.header {
  width: 50%;
  // border-bottom: 1px solid #e0e0e0;
  padding: 20px 0;
}

</style>
